param(
    $scenario = 'UnhandledExceptionScenario',
    $configuration = 'Debug',
    $mode='crash'
)

throw new 'Obsolete - use Get-DbgMemoryDump instead'

$erroractionpreference = 'stop';
pushd (split-path $myInvocation.MyCommand.Path);
. ..\ModuleParts\PowerDbg.Util.ps1
.\BuildScenarios.ps1

function EnsureEmptyDirectory($path){
    if (test-path $path) { remove-item $path -recurse };
    mkdir $path;
}

function WaitForFile($path, $pattern, [timespan]$timeout){
    $timer = [System.Diagnostics.StopWatch]::StartNew();
    do{
        start-sleep 0.5
        $files = @(dir $path -recurse | ? { $_.FullName -match $pattern })
    }while(($files.length -eq 0) -and ($timer.Elapsed -lt $timeout))
    if ($files.length -eq 0){
        dir $path -recurse
        throw 'Timed out waiting for files to be created'
    }
    # one more wait - file might not be completely written yet
    start-sleep 1
    $files
}

# Execute the required scenario, and capture the (appropriate) dump
# NB: Must specify an output directory with adplus.vbs
# If you don't specify an output directory, the default fails
# under UAC as the user won't (typically) have write permissions
# within the program files directory (where adplus output defaults to)
# NB2: -sc flag must come last !

# NB: Fully qualified path required for -sc parameter
$exe = "$pwd\ManagedScenarios\bin\$configuration\ManagedScenarios.exe"
AssertFileExists $exe

$scenarios = ,@{
    Name='UnhandledExceptionScenario';
    # LanchExe={};
    DebuggerArgs = "-$mode","-sc","$exe UnhandledExceptionScenario";
    }

if(@(get-process | ? { $_.Name -eq 'CDB' }).length -gt 0){
    # This is a nasty hack
    throw 'CDB is running. Currently I need it not running so I can try and wait for the dump to complete...'
}

foreach($scenario in $scenarios){
    $name = $scenario.Name
    $outDir = EnsureEmptyDirectory ".\Dumps\$name"

    write-host "Executing scenario $name"
    
    # Action to lauch scenario (if appropriate)
    if ($scenario.LanchExe){
        & $scenario.LanchExe
    }

    # Action to launch debugger and attach
    if (test-path "$debuggerRoot\adplus_old.vbs"){
        # For the time being, sniff the Windows 7 SDK version of Debugging Tools and use the old adplus script
        $args = "//nologo","$debuggerRoot\adplus_old.vbs","-quiet","-o","$outDir","-NoTList".split(',');    
    }elseif(test-path "$debuggerRoot\adplus.vbs"){
        # Use AdPlus.vbs from previous debugger versions
        $args = "//nologo","$debuggerRoot\adplus.vbs","-quiet","-o","$outDir","-NoTList".split(',');    
    }else{
        throw "AdPlus dump capture utility not found in $debuggerRoot"
    }
    
    $args+=$scenario.DebuggerArgs
    # cscript //nologo "$debuggerRoot\adplus.vbs" -quiet -o $outDir -NoTList $scenario.DebuggerArgs
    cscript $args

    # Depending on the process, it can take a while for the dump to happen
    # I think adplus runs CDB asyncronously
    # So current hacky solution is to sit and wait for it to finish...
    start-sleep 1
    while(@(get-process | ? { $_.Name -eq 'CDB' }).length -gt 0){
        write-host Waiting for dump to finish...
        start-sleep 1
    }
}
write-host Dump Complete -backgroundColor:Green

dir .\Dumps\ -recurse | ? { $_.Extension -eq '.dmp' } | select-object Name
popd